package com.magic.smartcity.view.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.magic.smartcity.R;
import com.magic.smartcity.beans.FeedbackBean;
import com.magic.smartcity.databinding.FragmentMeFeedbackListBinding;
import com.magic.smartcity.interfaces.IUserCallBack;
import com.magic.smartcity.interfaces.IUserPresenter;
import com.magic.smartcity.interfaces.base.BaseUserCallBackFragment;

import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MeFeedbackListFragment extends BaseUserCallBackFragment {


    private static final String TAG = "FeedbackListFragment";
    @Inject
    IUserPresenter mPresenter;
    private String mToken;

    private FragmentMeFeedbackListBinding mBinding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentMeFeedbackListBinding.inflate(getLayoutInflater());
        mPresenter.registerCallBack(this);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("login-user", Context.MODE_PRIVATE);
        mToken = sharedPreferences.getString("token", null);
        initData();
        return mBinding.getRoot();
    }

    private void initData() {
        if (mToken != null) {
            //获取反馈列表
            mPresenter.getFeedbackList();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

    @Override
    public void feedbackListLoaded(List<FeedbackBean> beanList) {
        List<String> titles = beanList.stream().map(FeedbackBean::getTitle).collect(Collectors.toList());
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getContext(), R.layout.support_simple_spinner_dropdown_item, titles);
        mBinding.listView.setAdapter(arrayAdapter);
        mBinding.listView.setOnItemClickListener((parent, view, position, id) -> {
            Bundle bundle = new Bundle();
            bundle.putInt("id",beanList.get(position).getId());
            Navigation.findNavController(view).navigate(R.id.action_feedbackListFragment_to_feedbackDetailFragment2,bundle);
        });
    }

    @Override
    public void error(String str) {

    }
}